package org.javaforever.cookieshop.controller;

import java.net.URLEncoder;
import java.util.List;

import org.javaforever.cookieshop.domain.CustOrderBean;
import org.javaforever.cookieshop.domain.GoodsBean;
import org.javaforever.cookieshop.domain.PageBean;
import org.javaforever.cookieshop.domain.TypeBean;
import org.javaforever.cookieshop.service.GoodsService;
import org.javaforever.cookieshop.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

@Controller
public class GoodsCustController { 
	@Autowired
	protected GoodsService goodsService;
	@Autowired
	protected TypeService typeService;
    
    @GetMapping("/goods_detail")
    public String goodsDetail(@RequestParam("id") Long id, Model model) throws Exception{
    	try {
            GoodsBean g = goodsService.findGoodsById(id);
            List<TypeBean> t = typeService.listActiveTypes();
            model.addAttribute("g",g);
            model.addAttribute("t",t);
            return "goods_detail";
        } catch (Exception e) {
        	e.printStackTrace();
        	return "goods_detail";
        }
    }
    
    @GetMapping("/goodsrecommend_list")
    public String goodsDetail(@RequestParam(value = "type", defaultValue = "0") Long type,@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber, Model model) throws Exception{
        if(pageNumber<=0)
            pageNumber=1;
        PageBean p = goodsService.getGoodsRecommendPage(type, pageNumber);

        if(p.getTotalPage()==0)
        {
            p.setTotalPage(1);
            p.setPageNumber(1);
        }
        else {
            if(pageNumber>=p.getTotalPage()+1)
            {
                p = goodsService.getGoodsRecommendPage(type, p.getTotalPage());
            }
        }
	    List<TypeBean>typeList= typeService.listActiveTypes();
	    model.addAttribute("typeList",typeList);
        model.addAttribute("p", p);
        model.addAttribute("t", type);
    	return "goodsrecommend_list";
    }
    
    
    @GetMapping("/goods_cart")
    public String goodsCart(HttpSession session, Model model) throws Exception{
		CustOrderBean order = (CustOrderBean)session.getAttribute("order");
	    List<TypeBean>typeList= typeService.listActiveTypes();
	    model.addAttribute("typeList",typeList);
		if (order != null) model.addAttribute("order",order);
        return "goods_cart";
    }
    
    @GetMapping("/goods_list")
    public String goodsList(@RequestParam(value = "type", defaultValue = "0") Long typeid,@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber, Model model) throws Exception{
        TypeBean t=null;
        if(typeid!=0)
        {
            t=typeService.findTypeById(typeid);
        }
        model.addAttribute("t",t);
        if(pageNumber<=0)
            pageNumber=1;
        PageBean p=goodsService.selectPageByTypeID(typeid,pageNumber);

        if(p.getTotalPage()==0)
        {
            p.setTotalPage(1);
            p.setPageNumber(1);
        }
        else {
            if(pageNumber>=p.getTotalPage()+1)
            {
                p=goodsService.selectPageByTypeID(typeid,p.getTotalPage());
            }
        }
	    List<TypeBean>typeList= typeService.listActiveTypes();
	    model.addAttribute("typeList",typeList);
        model.addAttribute("p",p);
        model.addAttribute("id",String.valueOf(typeid));
        return "goods_list";
    }
    
    @PostMapping("/goods_buy")
    public void goodsBuy(@RequestParam(value = "goodsid") Long goodsid, HttpServletResponse response,HttpSession session, Model model) throws Exception{
        CustOrderBean o = null;
        if(session != null && session.getAttribute("order") != null) {
            o = (CustOrderBean) session.getAttribute("order");
        }else {
            o = new CustOrderBean();
            session.setAttribute("order", o);
        }
        GoodsBean goods = goodsService.findGoodsById(goodsid);
        if(goods.getStock()>0) {
            o.addGoods(goods);
            response.getWriter().print("ok");
        }else {
            response.getWriter().print("fail");
        }
     }
    
    @GetMapping("/goods_search")
    public String goodsSearch(@RequestParam(value = "keyword", defaultValue = "") String keyword,@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber, Model model) throws Exception{
        if(pageNumber<=0)
        {
            pageNumber=1;
        }
        PageBean p =goodsService.getSearchGoodsPage(keyword,pageNumber);

        if(p.getTotalPage()==0)
        {
            p.setTotalPage(1);
            p.setPageNumber(1);
        }
        else {
            if(pageNumber>=p.getTotalPage()+1)
            {
                p =goodsService.getSearchGoodsPage(keyword,pageNumber);
            }
        }
        
	    List<TypeBean>typeList= typeService.listActiveTypes();
	    model.addAttribute("typeList",typeList);
        model.addAttribute("p",p);
        model.addAttribute("keyword", URLEncoder.encode(keyword,"utf-8"));
        return "goods_search";
    }
}
